﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocialPhotos.Models.Entities;

namespace SocialPhotos.Models.SqlExpressRepository
{
    public class SqlExUserRepository : IUserRepository
    {
        /// <summary>
        /// SQL Server Express Context
        /// </summary>
        private readonly EfDbContext _dbContext = new EfDbContext();

        public IQueryable<Entities.User> Users
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the name of the user by user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public User GetUserByUserName(string userName)
        {
            return this._dbContext.Users.Where(p => p.Name.Equals(userName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
        }

        /// <summary>
        /// Gets the user by provider key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public User GetUserByProviderKey(string key)
        {
            return this._dbContext.Users.Where(p => p.ProviderKey.Equals(key, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
        }

        /// <summary>
        /// Verifies the user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public bool VerifyUser(string userName, string password)
        {
            User user = this._dbContext.Users.Where(p => p.Name.Equals(userName, StringComparison.OrdinalIgnoreCase) && p.Password.Equals(password)).FirstOrDefault();
            return user != null;
        }

        /// <summary>
        /// Veryfies the user.
        /// </summary>
        /// <param name="priderKey">The prider key.</param>
        /// <returns></returns>
        public bool VeryfyUser(string priderKey)
        {
            User user = this._dbContext.Users.Where(p => p.ProviderKey.Equals(priderKey, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            return user != null;
        }

        public bool CreateUser(string providerKey, string name, string password)
        {
            User user = new User();
            user.ProviderKey = providerKey;
            user.Name = name;
            user.Password = password;
            this._dbContext.Users.Add(user);
            int result = this._dbContext.SaveChanges();
            return result > 0;
        }
    }
}
